<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 角色管理内容 -->
          <el-tab-pane label="角色管理">
            <el-row style="height: 60px">
              <!-- 新增角色按钮 -->
              <el-button
                type="primary"
                size="small"
                icon="el-icon-plus"
                @click="addRole"
                >新增角色</el-button
              >
            </el-row>
            <el-table border="" :data="list">
              <el-table-column
                header-align="left"
                align="center"
                type="index"
                label="序号"
                width="120px"
              ></el-table-column>
              <el-table-column
                header-align="left"
                align="center"
                prop="name"
                label="角色名"
                width="240px"
              ></el-table-column>
              <el-table-column
                header-align="left"
                align="center"
                prop="description"
                label="描述"
              ></el-table-column>
              <el-table-column align="center" header-align="left" label="操作">
                <template slot-scope="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="preBtnOK(row.id)"
                    >分配权限</el-button
                  >
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(row.id)"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click="deleteRole(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <el-pagination
                layout="prev, pager, next"
                :total="this.page.total"
                :page-size="this.page.pagesize"
                :page-count="this.page.page"
                @current-change="changePage"
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息" name="second">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              show-icon
              type="info"
              :closable="false"
            >
            </el-alert>
            <el-form
              style="padding-left: 60px; margin-top: 50px"
              label-width="120px"
            >
              <el-form-item label="企业名称">
                <el-input disabled style="width: 400px" v-model="company.name">
                </el-input>
              </el-form-item>
              <el-form-item label="公司地址" label-width="120px">
                <el-input
                  disabled
                  style="width: 400px"
                  v-model="company.companyAddress"
                ></el-input>
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input
                  disabled
                  style="width: 400px"
                  v-model="company.companyPhone"
                ></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  disabled
                  style="width: 400px"
                  v-model="company.mailbox"
                ></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  disabled
                  style="width: 400px"
                  type="textarea"
                  :rows="3"
                  v-model="company.remarks"
                ></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹出层 -->
      <el-dialog
        :visible="isShowDialog"
        :title="dialogTitleName"
        @close="isCanel"
      >
        <el-form
          :model="roleForm"
          :rules="rules"
          ref="roleForm"
          label-width="120px"
        >
          <el-form-item label="角色名" prop="name">
            <el-input
              prop="name"
              style="width: 80%"
              v-model="roleForm.name"
            ></el-input>
          </el-form-item>
          <el-form-item label="描述">
            <el-input
              style="width: 80%"
              v-model="roleForm.description"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-button @click="isCanel">取消</el-button>
          <el-button type="primary" @click="isOkbtn">确定</el-button>
        </el-row>
      </el-dialog>
      <el-dialog title="分配权限" :visible.sync="showPreDialog">
        <el-tree
          ref="preTree"
          :data="treeList"
          :props="defaultProps"
          :show-checkbox="true"
          :default-expand-all="true"
          :default-checked-keys="selectCheck"
          :check-strictly="true"
          node-key="id"
        >
        </el-tree>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnPreCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnPreOk"
              >确定</el-button
            >
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from "@/api/permission";
import { tranTree } from "@/utils";

import {
  getRoleList,
  getCompanyInfo,
  delRole,
  getRole,
  updateRole,
  addRole,
  assignPrem,
} from "@/api/setting";
import { mapGetters } from "vuex";
export default {
  name: "setting",
  data() {
    return {
      list: [],
      treeList: [], // 专门用来接收权限数据 树形数据
      roleId: null, // 用来记录分配角色的id
      selectCheck: [], // 定义一个数组来接收 已经选中的节点
      defaultProps: { label: "name" },
      page: { page: 1, pagesize: 10, total: 0 },
      company: [],
      roleForm: {
        name: "",
        description: "",
      },
      rules: {
        name: [{ required: true, message: "角色名不能为空", trigger: "blur" }],
      },
      isShowDialog: false, //新增角色弹层
      showPreDialog: false, //分配权限弹层
    };
  },
  computed: {
    ...mapGetters(["companyId"]),
    dialogTitleName() {
      return this.roleForm.id ? "编辑角色" : "新增角色";
    },
  },
  created() {
    this.getRoleList();
    this.getCompanyInfo();
  },
  methods: {
    //获取所有角色信息
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page);
      this.list = rows;
      this.page.total = total;
    },
    //切换页面更新数据
    changePage(newpage) {
      this.page.page = newpage;
      this.getRoleList();
    },
    //获取公司信息
    async getCompanyInfo() {
      this.company = await getCompanyInfo(this.companyId);
    },
    //删除角色
    async deleteRole(id) {
      try {
        await this.$confirm("是否要删除该角色");
        await delRole(id);
        this.getRoleList();
        this.$message.success("删除成功");
      } catch (err) {
        console.log(err);
      }
    },
    //点击编辑弹出层
    async editRole(id) {
      this.isShowDialog = true;
      this.roleForm = await getRole(id);
    },
    //点击新增角色弹出层
    async addRole() {
      this.isShowDialog = true;
    },
    async isOkbtn() {
      try {
        //提交编辑校验结果
        await this.$refs.roleForm.validate();
        if (this.roleForm.id) {
          //如果有ID就是编辑 编辑角色
          await updateRole(this.roleForm);
        } else {
          //没有就是添加角色
          await addRole(this.roleForm);
        }
        this.isShowDialog = false;
        //重新渲染界面
        this.getRoleList();
      } catch (err) {}
    },
    //关闭弹层清楚表单信息和验证
    isCanel() {
      this.roleForm = {
        name: "",
        description: "",
      };
      this.isShowDialog = false;
      this.$refs.roleForm.resetFields();
    },
    //点击分配权限弹层
    async preBtnOK(id) {
      this.treeList = tranTree(await getPermissionList(), 0);
      this.roleId = id;
      const { permIds } = await getRole(id); // permIds是当前角色所拥有的权限点数据
      this.selectCheck = permIds;
      this.showPreDialog = true;
    },
    //提交分配权限信息
    async btnPreOk() {
      // console.log(this.roleId);
      console.log(this.$refs.preTree.getCheckedKeys());
      await assignPrem({
        id: this.roleId,
        permIds: this.$refs.preTree.getCheckedKeys(),
      });
      this.showPreDialog = false;
    },
    // 取消提交分配权限信息
    btnPreCancel() {
      this.selectCheck = []; // 重置数据
      this.showPreDialog = false;
    },
  },
};
</script>

<style>
</style>